//
//  HomePageViewController.m
//  WuLi
//
//  Created by Gollum on 16/8/29.
//  Copyright © 2016年 Gollum. All rights reserved.
//

#import "HomePageViewController.h"
#import "JHMacro.h"
#import "TopScrollMenu.h"
#import "RecommendViewController.h"
#import "VideoViewController.h"
#import "MJRefresh.h"
#import "RankingListViewController.h"

#import "JHNewGuideView.h"
@interface HomePageViewController ()<TopScrollMenuDelegate,UIScrollViewDelegate>
@property (nonatomic, strong) TopScrollMenu *topScrollMenu;
@property (nonatomic, strong) UIScrollView *mainScrollView;
@property (nonatomic, strong) NSArray *titles;

//新手引导
@property (strong, nonatomic) JHNewGuideView *guideView;

@end

@implementation HomePageViewController
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
//    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self setupChildViewController];


    self.titles = @[@"推荐", @"视频", @"排行榜"];
   
    self.topScrollMenu = [TopScrollMenu topScrollMenuWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];
    
    
    _topScrollMenu.staticTitleArr = [NSArray arrayWithArray:_titles];
    
    _topScrollMenu.topScrollMenuDelegate = self;
    
    [self.view addSubview:_topScrollMenu];
    
    // 创建底部滚动视图
    self.mainScrollView = [[UIScrollView alloc] init];
    _mainScrollView.frame = CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height);
    _mainScrollView.contentSize = CGSizeMake(self.view.frame.size.width * _titles.count, 0);
    _mainScrollView.backgroundColor = [UIColor clearColor];
    // 开启分页
    _mainScrollView.pagingEnabled = YES;
    // 没有弹簧效果
    _mainScrollView.bounces = NO;
    // 隐藏水平滚动条
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    // 设置代理
    _mainScrollView.delegate = self;
    [self.view addSubview:_mainScrollView];
    
    [self showVc:0];
    
    [self.view insertSubview:_mainScrollView belowSubview:_topScrollMenu];
    
//     recommendVC.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    
}






#pragma mark - - - TopScrollMenu代理方法
- (void)TopScrollMenu:(TopScrollMenu *)topScrollMenu didSelectTitleAtIndex:(NSInteger)index{
    
    // 1 计算滚动的位置
    CGFloat offsetX = index * self.view.frame.size.width;
    self.mainScrollView.contentOffset = CGPointMake(offsetX, 0);
    if (offsetX == 0) {
        [self showVc:0];
    }else{
    // 2.给对应位置添加对应子控制器
    [self showVc:index];
    }
    
}

- (void)setupChildViewController {
    // 精选
    RecommendViewController *recommendVC = [[RecommendViewController alloc] init];
    [self addChildViewController:recommendVC];
    
    // 电视剧
    VideoViewController *videoVC = [[VideoViewController alloc] init];
    [self addChildViewController:videoVC];
    
    // 电影
    RankingListViewController *rankingVC = [[RankingListViewController alloc] init];
    [self addChildViewController:rankingVC];

}

// 显示控制器的view
- (void)showVc:(NSInteger)index {
    
    CGFloat offsetX = index * self.view.frame.size.width;
    
    UIViewController *vc = self.childViewControllers[index];
    
    // 判断控制器的view有没有加载过,如果已经加载过,就不需要加载
    if (vc.isViewLoaded) return;
    
    [self.mainScrollView addSubview:vc.view];
    
    vc.view.frame = CGRectMake(offsetX, 0, self.view.frame.size.width, self.view.frame.size.height);
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    // 计算滚动到哪一页
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    
    // 1.添加子控制器view
    [self showVc:index];
    
    // 2.把对应的标题选中
    UILabel *selLabel = self.topScrollMenu.allTitleLabel[index];
    
    [self.topScrollMenu staticTitleLabelSelecteded:selLabel];
    
    
    //  [self.topScrollMenu scrollTitleLabelSelecteded:selLabel];
    
    // 3.让选中的标题居中
    //[self.topScrollMenu scrollTitleLabelSelectededCenter:selLabel];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





@end
